<?php
	class CategoryModel extends Model{
		// 定义校验规则
		public $validate=array(
			array('cname','nonull','栏目名称不能为空',2,3),
			);
		// 定义显示所有方法
		public function allCategory(){
			$category=$this->all();
			return $category;
		}
		// 定义添加方法
		public function addCategory(){
			if($this->create()){
				if($this->add()){
					$this->cacheData();
					return true;
				}else{
					return false;
				}
			}
		}
		//定义处理数组的方法
		public function getTreeCategory(){
			return Data::tree($this->order('sort')->all(),'cname');
		}
		//获取数据
		public function getCategoryByCid(){
			$cid=Q('cid',0,'intval');
			return $data=$this->where(array('cid'=>$cid))->find();
		}
		//获取处理后的数据
		public function getEditCategory(){
			$data=$this->getTreeCategory();
			$pid=Q('cid',0,'intval');
			foreach ($data as $k => $v) {
				$disabled=(Data::isChild($data,$v['cid'],$pid) or $v['cid']==$pid) ?' disabled="" ' : '';
				$data[$k]['disabled'] = $disabled;
			}
			return $data;
		}
		// 定义保存方法
		public function saveCategory(){
			$cid=Q('cid',0,'intval');
			if($this->create()){
				if($this->where("cid='$cid'")->save()){
					$this->cacheData();
					return true;
				}else{
					return false;
				}
			}
			
		}
		public function cacheData(){
			$data=$this->getTreeCategory();
			return S('category',$data);
		}
	}
?>